package logic

import (
	"ahutoj/web/dao"
	"ahutoj/web/io/constanct"
	rediscache "ahutoj/web/cache/redis"
	"ahutoj/web/io/request"
	"ahutoj/web/io/response"
	"ahutoj/web/middlewares"
	"ahutoj/web/models"
	"ahutoj/web/utils"
	"fmt"
	"sort"
	"github.com/gin-gonic/gin"
)

func GetNotice(ctx *gin.Context, req *request.GetNoticeReq) (interface{}, error) {
	notice, err := models.GetNotice(ctx, req.ID)
	if err != nil {
		return nil, err
	}
	fmt.Println(notice)
	if notice.NID != req.ID {
		return response.CreateResponseStr(constanct.AUTH_LOGIN_UIDNotExistCode, "这个公告被删除或者不存在", response.ERROR), nil
	}
	return response.GetNoticeResp{
		Response: response.CreateResponse(constanct.SuccessCode),
		GetNOticeItem: response.GetNOticeItem{
			UID:         notice.UID,
			ID:          notice.NID,
			Title:       notice.Title,
			Content:     notice.Content,
			CreatedTime: notice.CreateTime,
			UpdatedTime: notice.UpdateTime,
		},
	}, nil
}

func GetNoticeList(ctx *gin.Context) (interface{}, error) {
	notices, err := models.GetAllNotices(ctx)
	if err != nil {
		return nil, err
	}
	data := make([]response.GetNOticeItem, 0)
	for _, notice := range notices {
		data = append(data, response.GetNOticeItem{
			UID:         notice.UID,
			ID:          notice.NID,
			Title:       notice.Title,
			Content:     notice.Content,
			CreatedTime: notice.CreateTime,
			UpdatedTime: notice.UpdateTime,
		})
	}
	return response.GetListNoticeResp{
		Response: response.CreateResponse(constanct.SuccessCode),
		Count:    len(notices),
		Data:     data,
	}, nil
}

func CreateNotice(ctx *gin.Context, req *request.CreateNoticeReq) (interface{}, error) {
	notice := dao.Notice{
		UID:        middlewares.GetUid(ctx),
		Title:      req.Title,
		Content:    req.Content,
		CreateTime: utils.GetNow(),
		UpdateTime: 0,
		IsDelete:   false,
	}
	if models.NoticeEqualLastSource(ctx, req.Title, req.Content) {
		return response.CreateResponseStr(constanct.NOTICE_GETNOTICE_FAILED, "禁止频繁发送重复公告", response.WARNING), nil
	}
	err := models.CreateNotice(ctx, notice)
	if err != nil {
		return nil, err
	}
	return response.CreateResponse(constanct.SuccessCode), nil
}

func UpdateNotice(ctx *gin.Context, req *request.UpdateNoticeReq) (interface{}, error) {
	notice := dao.Notice{
		NID:        req.ID,
		Title:      req.Title,
		Content:    req.Content,
		UpdateTime: utils.GetNow(),
	}
	err := models.UpdateNotice(ctx, notice)
	if err != nil {
		return nil, err
	}
	return response.CreateResponse(constanct.SuccessCode), nil
}

func DeleteNotice(ctx *gin.Context, req *request.DeleteNoticeReq) (interface{}, error) {
	notice := dao.Notice{
		NID:      req.ID,
		IsDelete: true,
	}
	err := models.UpdateNotice(ctx, notice)
	if err != nil {
		return nil, err
	}
	return response.CreateResponse(constanct.SuccessCode), nil
}
func GetNoticeImages(ctx *gin.Context)(interface{}, error){//最多保存三张，按照时间倒叙展示
	req:=request.GetObjectsReq{
		BucketName: "notice-images",
		Prefix: "notice_images_",
	}
	data,err:=GetObjects(ctx,&req)
	if err != nil {
		return response.CreateResponse(constanct.NOTICE_GETNOTICE_FAILED),err
	}
	sort.Slice(data.ObjectInfo, func(i, j int) bool {
		return data.ObjectInfo[i].LastModified.After(data.ObjectInfo[j].LastModified)
	})
	Len:=len(data.ObjectInfo)
	if Len>3{
		Len=3
	}
	images:=make([]response.GetObjectResp,Len)
	for i:=0;i<Len;i++{
		Object,_:=GetObject(ctx,&request.GetObjectReq{
			BucketName: "notice-images",
			ObjectName: data.ObjectInfo[i].Key,
			GetObjectType:request.Base64,
			})
		images[i]=response.GetObjectResp{
		    ObjectData:ToGetObjectResp(Object).ObjectData,
		}
	}
	return response.GetListNoticeImagesResp{
		Response: response.CreateResponse(constanct.SuccessCode),
		Count:    Len,
		Data:     images,
	}, nil
}

func CreateNoticeImage(ctx *gin.Context,req *request.CreateNoticeImageReq)(interface{}, error){
	putinfo:=request.PutObjectReq{
		BucketName: "notice-images",
		ObjectName: "notice_images_"+utils.BuildMD5FileName(req.File),
		File: req.File,
	}
	_,err:=PutObject(ctx,&putinfo)
	if err!=nil{
		return nil,err
	}
	return response.CreateResponse(constanct.SuccessCode),nil
}
func GetData(ctx *gin.Context)(interface{},error){
	logger:=utils.GetLogInstance()
	var err error
	resp:=response.GetDataResp{
		Response: response.CreateResponse(constanct.SuccessCode),
	}
	resp.ProblemNumber,err = rediscache.GetProblemCount(ctx)
	if err!=nil {
		logger.Errorf("call GetProblemCount failed,err=%s", err.Error())
		return nil,err
	}
	resp.UserNumber= models.GetUserCount(ctx)
	resp.TotalSubmit,err=rediscache.GetSubmitCount(ctx)
	if err!=nil {
		logger.Errorf("call GetSubmitCount failed,err=%s", err.Error())
		return nil,err
	}
	resp.TodaySubmit,err=models.GetTodaySubmitCount(ctx,dao.Submit{})
	if err!=nil {
		logger.Errorf("call GetTodaySubmitCount failed,err=%s", err.Error())
		return nil,err
	}
	return resp,nil
}